package practicedemo35;

import java.io.IOException;
import java.util.Scanner;


//有问题，但是只是一个练习，有错误很正常，java的数据操作没有提供相应的接口，这样的数据处理操作时SQL执行的
public class StudentTest {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        Student[] students = new Student[100];
        int index = 3;
        students[0] = new Student("20221003024","小胡",20);
        students[1] = new Student("20221003025","小明",23);
        students[2] = new Student("20221003026","小红",22);

        System.out.println("按1,添加一个学生对象");
        System.out.println("按2,通过id删除学生的信息");
        System.out.println("按3,打印存储的学生信息");
        System.out.println("按4,查询学生的名字,并且将他的年龄进行加1");
        System.out.println("按5,退出主程序");

        while (true){
            int key = sc.nextInt();
            switch (key){
                case 1:
                    students[index] = Int_Student();
                    index++;
                    break;
                case 2:
                    System.out.println("请输入要删除学生的学号");
                    String id = sc.next();
                    int judge = 0;
                    for (int i = 0; i < index; i++) {
                        if (id.equals(students[i].getId())){
                            judge = i;
                            for (int j = judge; j < index-1; j++) {
                                students[judge] = students[judge+1];
                            }
                            students[index-1] = null;
                            System.out.println("删除成功");
                            index--;
                            break;
                        }
                        System.out.println("不存在学生");
                        break;
                    }
                    break;
                case 3:
                    for (int i = 0; i < index; i++) {
                        System.out.println(students[i].getId()+'\t'+students[i].getName()+'\t'+students[i].getAge()+'\t');
                    }
                    break;
                case 4:
                    System.out.println("输入查询的学生姓名");
                    String name = sc.next();
                    for (int i = 0; i < (index - 1); i++) {
                        if (name.equals(students[i].getName())){
                            students[i].setAge(students[i].getAge()+1);
                            System.out.println("所选学生年龄成功加1");
                            break;
                        }
                    }
                    break;
                case 5:
                    System.exit(0);
                    break;
                default:
                    System.out.println("输入了错误的选项，请重新输入");
                    break;
            }
        }
    }

    public static Student Int_Student(){

        Scanner sc = new Scanner(System.in);
        Student student = new Student();
        System.out.println("请输入学生学号");
        student.setId(sc.nextLine());
        System.out.println("请输入学生姓名");
        student.setName(sc.nextLine());
        System.out.println("请输入学生年龄");
        student.setAge(sc.nextInt());
        System.out.println("成功录入！！");
        return student;
    }
}
